home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr11 / powerb5.zip / P5UTL000.TIP < prev    next >
Text File  |  1993-06-01  |  3KB  |  85 lines

  1. Programs often need to know what key the user just pressed.
  2. If the key in question happens to be a letter, number, or
  3. punctuation symbol, your BASIC programs should have no
  4. trouble recognizing it from its ASCII code. But function and
  5. cursor-control keys don't have ASCII codes. Your program
  6. must recognize them by their keyboard scan codes -- special
  7. codes that are assigned to keystrokes on the IBM PC and
  8. compatibles.
  9.  
  10. To find out quickly how to recognize any key, I wrote
  11. KEY.BAS. To use it, run KEY.BAS from QuickBASIC or the DOS
  12. 5.0 QBASIC, and press the key you want to detect. If the key
  13. can be detected only by its scan code, that's what you'll
  14. see on the left, otherwise, you will see its ASCII code on
  15. the right.
  16.  
  17. Yaniv Kunda
  18. Ramat-Hasharon, Israel
  19.  
  20. Editor's Note: The file KEY.BAS appears in the P5UTL
  21. directory of your PowerBase *.* Volume 5 diskette. I've
  22. enhanced Mr. Kunda's program to add an arrow next to the
  23. code for the most recent key typed and to make the screen
  24. display easier to understand. To exit the program at any
  25. time, press <Ctrl>-<Break>.
  26.  
  27. This tip works because of the QuickBASIC INKEY$ function's
  28. idiosyncrasies. INKEY$ returns an empty string if no key has
  29. been pressed, a string with a single character if an
  30. ordinary key was pressed, or a string with two characters if
  31. a special key (such as a function key) was pressed. If an
  32. ordinary key was pressed, the string contains its ASCII
  33. code. In the case of a special key, the first character of
  34. the string is always a NUL (ASCII code 0), and the second
  35. contains the scan code of the key.
  36.  
  37. One way to interpret the results of the INKEY$ function in
  38. your own program is shown in the listing below. It uses an
  39. ON...GOTO statement to look repeatedly for keyboard input
  40. and determine what kind of key, if any, was pressed.
  41.  
  42. When the user presses a character key, the program jumps to
  43. the ORDINARY label; when the user presses a function or
  44. cursor-control key, the program goes to SPECIAL. The code in
  45. LISTING 2 shows what might follow an ORDINARY label where
  46. the user is allowed to press A, B, or C to choose a menu
  47. item, as well as a SPECIAL label where he or she can press
  48. <F1> through <F10>.
  49.  
  50. ---- BEGIN LISTING ----
  51. REM This code fragment shows how to process incoming keystrokes
  52. REM which might include either normal ASCII characters or
  53. REM function keys (but no other special keys).
  54.  
  55. POLL:
  56.   A$ = INKEY$: IF A$ = "" THEN GOTO POLL
  57.   IF LEN(A$) > 1 THEN GOTO SPECIAL
  58.  
  59. ORDINARY:
  60.   REM Convert key to a number: 1 for A, 2 for B, etc.
  61.   ORDKEY = (ASC(A$) - ASC("A") + 1)
  62.   REM Check to see if key is legal
  63.   IF ORDKEY <= 0 or ORDKEY > ASC("C") THEN GOTO POLL
  64.   REM Go to the routine that processes the key
  65.   ON ORDKEY GOTO AKEY, BKEY, CKEY
  66.  
  67. SPECIAL:
  68.   REM Get scan code out of A$.
  69.   FKEY = ASC(RIGHT$(A$,1))
  70.   REM Deduct 58 from the scan code.
  71.   REM This yields 1 for F1, 2 for F2, etc.
  72.   REM Reject other special keys
  73.   IF FKEY <= 0 OR FKEY > 10 THEN GOTO POLL
  74.   REM Go to the routine that processes the key
  75.   ON FKEY GOTO F1,F2,F3,F4,F5,F6,F7,F8,F9,F10
  76. ---- END LISTING ----
  77.  
  78.  
  79. Title: BASIC Key Code Decoder
  80. Category: UTL
  81. Issue Date: February, 1992
  82. Editor: Brett Glass
  83. Supplementary Files: P5UTL\KEY.BAS
  84. Filename: P5UTL000.TIP
  85.